﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;

namespace GSSoftSL.ViewModel.HocVienVM
{
    public class HocVienSearchViewModel : ViewModelBase
    {
        private GSSoftContext _dc;

        
        private HocVien _current = null;
        public HocVien Current
        {
            get
            {
                return _current;
            }
            set
            {
                if (_current == value)
                {
                    return;
                }
                _current = value;
                RaisePropertyChanged("Current");
            }
        }

        /// <summary>
        /// Initializes a new instance of the HocVienSearchViewModel class.
        /// </summary>
        public HocVienSearchViewModel()//GSSoftContext context)
        {
            _dc = ServiceLocator.Current.GetInstance<DomainContext>("HocVien") as GSSoftContext;// context;
            HocVienProxy = new HocVienSearchProxy();
        }

        
        private IEnumerable<HocVien> _hocViens = null;
        public IEnumerable<HocVien> HocViens
        {
            get
            {
                return _hocViens;
            }
            set
            {
                if (_hocViens == value)
                {
                    return;
                }
                _hocViens = value;
                RaisePropertyChanged("HocViens");
            }
        }

        #region Command
        

        private RelayCommand _searchCommand;
        public RelayCommand SearchCommand
        {
            get
            {
                return _searchCommand ??
                       (_searchCommand = new RelayCommand(
                                             () =>
                                                 {
                                                     var query = _dc.GetHocViensQuery();
                                                     if (_hocVienProxy.MaHV != null && _hocVienProxy.MaHV.Trim() != "")
                                                         query =
                                                             query.Where(n => n.MaHV.Contains(_hocVienProxy.MaHV));
                                                     if (_hocVienProxy.HoLot != null && _hocVienProxy.HoLot.Trim() != "")
                                                         query =
                                                             query.Where(n => n.HoLot.Contains(_hocVienProxy.HoLot));
                                                     if (_hocVienProxy.TenHV != null && _hocVienProxy.TenHV.Trim() != "")
                                                         query =
                                                             query.Where(n => n.Ten.Contains(_hocVienProxy.TenHV));
                                                     if (_hocVienProxy.Email != null && _hocVienProxy.Email.Trim() != "")
                                                         query =
                                                             query.Where(n => n.Email.Contains(_hocVienProxy.Email));
                                                     if (_hocVienProxy.DienThoai != null && _hocVienProxy.DienThoai.Trim() != "")
                                                         query =
                                                             query.Where(n => n.DTCaNhan.Contains(_hocVienProxy.DienThoai));
                                                     if (_hocVienProxy.GioiTinh != null)
                                                         query =
                                                             query.Where(n => n.GioiTinh == _hocVienProxy.GioiTinh);
                                                     /*var query = _dc.GetHocVienFiltersQuery(_hocVienProxy.MaHV.Trim(),
                                                                                            _hocVienProxy.HoLot.Trim(),
                                                                                            _hocVienProxy.TenHV.Trim(),
                                                                                            _hocVienProxy.GioiTinh,
                                                                                            _hocVienProxy.Email.Trim(),
                                                                                            _hocVienProxy.DienThoai.Trim
                                                                                                ());*/
                                                     _dc.Load<HocVien>(query, LoadBehavior.RefreshCurrent, LoadHocVien_Completed, null);
                                                 },
                                             () => true));
            }
        }

        private RelayCommand _clearCommand;
        public RelayCommand ClearCommand
        {
            get
            {
                return _clearCommand
                    ?? (_clearCommand = new RelayCommand(
                                          () =>
                                              {
                                                  _hocVienProxy.MaHV =
                                                      _hocVienProxy.HoLot =
                                                      _hocVienProxy.TenHV =
                                                      _hocVienProxy.DienThoai = _hocVienProxy.Email = "";
                                                  _hocVienProxy.GioiTinh = null;
                                              }));
            }
        }

        #endregion

        private void LoadHocVien_Completed(LoadOperation<HocVien> loadHV)
        {
            HocViens = loadHV.Entities;
        }


        private HocVienSearchProxy _hocVienProxy;
        public HocVienSearchProxy HocVienProxy
        {
            get
            {
                return _hocVienProxy;
            }
            set
            {
                if (_hocVienProxy == value)
                {
                    return;
                }
                _hocVienProxy = value;
                RaisePropertyChanged("HocVienProxy");
            }
        }
    }

    public class HocVienSearchProxy : Entity
    {

        private string _maHV = "";
        public string MaHV
        {
            get
            {
                return _maHV;
            }
            set
            {
                if (_maHV == value)
                {
                    return;
                }
                _maHV = value;
                RaisePropertyChanged("MaHV");
            }
        }


        private string _hoLot = "";
        public string HoLot
        {
            get
            {
                return _hoLot;
            }
            set
            {
                if (_hoLot == value)
                {
                    return;
                }
                _hoLot = value;
                RaisePropertyChanged("HoLot");
            }
        }

        private string _tenHV = "";
        public string TenHV
        {
            get
            {
                return _tenHV;
            }
            set
            {
                if (_tenHV == value)
                {
                    return;
                }
                _tenHV = value;
                RaisePropertyChanged("TenHV");
            }
        }


        private string _dienThoai = "";
        public string DienThoai
        {
            get
            {
                return _dienThoai;
            }
            set
            {
                if (_dienThoai == value)
                {
                    return;
                }
                _dienThoai = value;
                RaisePropertyChanged("DienThoai");
            }
        }

        private string _email = "";
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                if (_email == value)
                {
                    return;
                }
                _email = value;
                RaisePropertyChanged("Email");
            }
        }

        private Nullable<int> _gioiTinh;
        public Nullable<int> GioiTinh
        {
            get
            {
                return _gioiTinh;
            }
            set
            {
                if (_gioiTinh == value)
                {
                    return;
                }
                _gioiTinh = value;
                RaisePropertyChanged("GioiTinh");
            }
        }
    }

}